package pack08_covid_grouping;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * @author lilulu
 * @date 2023/2/11 14:23
 */

//1：定义类继承WritableComparator 类（注意这个是，而WritableComparable接口）
public class MyGrouping extends WritableComparator {
    //2：在该类的构造方法中要调用父类的构造
    public MyGrouping() {
        super(CovidGroupingBean.class, true);//允许父类通过反射来创建JavaBaen的对象
    }

    //3：重写compare方法，自定义分组规则
    /*
      我们的分组规则：
         只要两个CovidGroupingBean的州（state）相同,compare就返回0，则就应该在一组
     */

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        CovidGroupingBean c1 = (CovidGroupingBean) a;
        CovidGroupingBean c2 = (CovidGroupingBean) b;

        return c1.getState().compareTo(c2.getState()); // 1  0  -1

    }
}
